home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 116 / MacAddict 116 (Mac Power Pack)(theDISC)(April 2006).iso / Software / Utilities / iGet2.0.3.dmg / iGet.app / Contents / Resources / macdiff.pl < prev    next >
Encoding:
Perl Script  |  2006-01-19  |  4.4 KB  |  198 lines

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use Getopt::Long;
  5.  
  6. $ENV{'PATH'} = '/Developer/Tools:' . $ENV{'PATH'};
  7.  
  8. my $result;
  9. my $usage = "usage: $0 <file1> <file2>\n";
  10. my $rezHack = "/..namedfork/rsrc";
  11. my $headerPrinted;
  12. my $indent = "  ";
  13. my $skipPartialFiles;
  14.  
  15. GetOptions('P' => \$skipPartialFiles);
  16.  
  17. my $file1 = shift @ARGV;
  18. my $file2 = shift @ARGV;
  19.  
  20. unless($file1 && $file2) {
  21.     print $usage;
  22.     exit(1);
  23. }
  24.  
  25. if(-l $file1 || -l $file2) {
  26.     my $result = compareSymlinks($file1, $file2);
  27.     exit($result);
  28. }
  29.  
  30. die "$file1 does not exist" unless -e $file1;
  31. die "$file2 does not exist" unless -e $file2;
  32.  
  33. if (-d $file1 || -d $file2) {
  34.     my $result = compareDirectories($file1, $file2);
  35.     exit($result);
  36. }
  37.  
  38. ########### Metadata
  39.  
  40. my $m1 = "/tmp/macdiff.1." . $$;
  41. my $m2 = "/tmp/macdiff.2." . $$;
  42.  
  43. my $metadata1 = readMetadata($file1);
  44. my $metadata2 = readMetadata($file2);
  45.  
  46. if($skipPartialFiles) {
  47.     foreach($metadata1, $metadata2) {
  48.         if(/type: "iGpf"/ && /creator: "iGet"/) {
  49.             print STDERR "$0: Skipping partial file comparison\n";
  50.             exit 0;
  51.         }
  52.     }
  53. }
  54.  
  55. writeMetadata($metadata1, $m1);
  56. writeMetadata($metadata2, $m2);
  57.  
  58. my $metadataDifferences = `diff --suppress-common-lines -y -W 70 $m1 $m2`;
  59. unlink $m1, $m2;
  60.  
  61. if($metadataDifferences) {
  62.     printHeader();
  63.     print STDERR "Metadata differences:\n";
  64.     my @lines = split("\n", $metadataDifferences);
  65.     print STDERR $indent, join("\n" . $indent, @lines), "\n";
  66.     $result = 1;
  67. }
  68.  
  69. ########### Data fork
  70.  
  71. compareFiles($file1, $file2, "Data fork", 1) || { $result = 1 };
  72.  
  73. ########### Resource fork
  74.  
  75. compareFiles($file1 . $rezHack, $file2 . $rezHack, "Resource fork") || { $result = 1 };
  76.  
  77. $headerPrinted && printBar();
  78.  
  79. exit $result;
  80.  
  81. ########### Subroutines
  82.  
  83. sub readMetadata {
  84.  
  85.     my ($inputFile, $tmpFile) = @_;
  86.  
  87.     my $metadataCommand = "GetFileInfo " . quotemeta($inputFile) . " | tail +2";
  88.     my $metadata = `$metadataCommand`;
  89.     $? && die("GetFileInfo returned error");
  90.  
  91.     return $metadata;
  92. }
  93.  
  94. sub writeMetadata {
  95.     my ($data, $file) = @_;
  96.     open TMP, ">$file";
  97.     print TMP $data;
  98.     close TMP;
  99. }
  100.  
  101. sub compareFiles {
  102.     my ($localFile1, $localFile2, $title, $compareMode) = @_;
  103.  
  104.     my @differences;
  105.     my ($f1mode, $f1length) = (stat($localFile1))[2,7];
  106.     my ($f2mode, $f2length) = (stat($localFile2))[2,7];
  107.         
  108.     if ($f1length != $f2length) {
  109.         push @differences, "lengths ($f1length, $f2length)";
  110.     }
  111.     
  112.     system("cmp", "-s", $localFile1, $localFile2);
  113.     my $cmpResult = ($? >> 8);
  114.     
  115.     if($cmpResult) {
  116.         push @differences, "contents";
  117.     }
  118.  
  119.     if($compareMode && ($f1mode != $f2mode)) {
  120.         push @differences, "perms ($f1mode, $f2mode)";
  121.     }
  122.     
  123.     if(@differences) {
  124.         printHeader();
  125.         print STDERR $title, " diffs: ", join(", ", @differences), "\n";
  126.         return 0;
  127.     }
  128.     
  129.     return 1;
  130. }
  131.  
  132. sub printHeader {
  133.     $headerPrinted && return;
  134.     
  135.     printBar();
  136.     print STDERR "macdiff found differences between the following items:\n";
  137.     print STDERR $indent, $file1, "\n";
  138.     print STDERR $indent, $file2, "\n";
  139.     $headerPrinted = 1;
  140. }
  141.  
  142. sub printBar {
  143.     print STDERR "***************************************************************************\n";
  144. }
  145.  
  146. sub compareSymlinks {
  147.     my ($file1, $file2) = @_;
  148.     
  149.     if((-l $file1) != (-l $file2)) {
  150.         printHeader();
  151.         print STDERR "one is a symlink and the other isn't\n";
  152.         printBar();
  153.         return 1;
  154.     }
  155.     
  156.     my $dest1 = readlink($file1);
  157.     my $dest2 = readlink($file2);
  158.     if($dest1 ne $dest2) {
  159.         printHeader();
  160.         print ETDERR "symlink targets don't match:\n";
  161.         print $indent, "$file1 -> $dest1\n", $indent, "$file2 -> $dest2\n";
  162.         printBar();
  163.         return 1;
  164.     }
  165.     return 0;
  166. }
  167.  
  168. sub compareDirectories {
  169.     my ($dir1, $dir2) = @_;
  170.     
  171.     if((-d $dir1) != (-d $dir2)) {
  172.         printHeader();
  173.         print STDERR "one is a folder and the other isn't\n";
  174.         printBar();
  175.         return 1;
  176.     }
  177.     
  178.     my ($f1mode, $f1date) = (stat($dir1))[2,9];
  179.     my ($f2mode, $f2date) = (stat($dir2))[2,9];
  180.     
  181.     my @differences;
  182.     if($f1mode != $f2mode) {
  183.         push @differences, "perms ($f1mode, $f2mode)";
  184.     }
  185.     
  186.     if($f1date != $f2date) {
  187.         push @differences, "mod date ($f1date, $f2date)";
  188.     }
  189.     
  190.     if(@differences) {
  191.         printHeader();
  192.         print STDERR "differences: ", join(", ", @differences), "\n";
  193.         return 1;
  194.     }
  195.     
  196.     return 0;
  197. }
  198.